import store from '@/store' // 导入你的 Vuex Store
import config from '@/common/config'
const requestInterceptors = () => {
	uni.$u.http.interceptors.request.use((configParam) => {
		configParam.data = configParam.data || {}
		configParam.header['Authorization'] = 'Bearer ' + uni.getStorageSync('token');
		configParam.header['clientid'] = config.clientId;
		return configParam
	}, (configParam) => Promise.reject(configParam))
	
}
const responseInterceptors = () => {

	uni.$u.http.interceptors.response.use((result) => {
		console.log(result)
		const res = result.data;
		console.log("我是拦截器")
		console.log(store.getters.token)
		if (res.code !== 200) {
			if (res.code == 401) {
				if(store.getters.token)
				{
					store.dispatch('Logout')
				}
				uni.navigateTo({
					url: '/pages/login/login',
					success: () => {
						uni.$u.toast("未登录，请先登录");
					}
				})
				
			} else {
				uni.$u.toast(res.msg);
			}
			return new Promise(() => {})
		}
		return res || {}
	}, (err) => {
		return Promise.reject(err)
	})
}


export {
	requestInterceptors,
	responseInterceptors
}
